<?php
namespace common\services;

use common\base\BaseActiveRecord;
use common\helpers\TStringHelper;
use common\models\Exhibition;
use common\models\ExhibitionExhibitor;
use common\models\Exhibitor;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;

class ExhibitionExhibitorServices extends ExhibitionExhibitor
{

    /**
     * 返回对应展会的知名展商
     * @param $exhibition_id
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getExhibitorsByExhibitionId($exhibition_id) {
        if (empty($exhibition_id)) return [];
        $model = ExhibitionExhibitor::find(false);
        $model->andFilterWhere(['=', 'status', ExhibitionExhibitor::STATUS_FLAG_NORMAL])
            ->andFilterWhere(['=', 'exhibition_id', $exhibition_id]);

        $res = $model->asArray()->select("exhibitor_id,name")->orderBy("order desc,id asc")->all();

        return $res;
    }

    /**
     * 保存知名展商数据
     * @param $exhibition_id
     * @param $data
     * @return bool
     */
    public function saveExhibitorData($exhibition_id, $data) {
        //将对应展会数据设置为停用
        ExhibitionExhibitor::updateAll(['status' => ExhibitionExhibitor::STATUS_FLAG_STOP],'exhibition_id=:exhibition_id', [':exhibition_id' => $exhibition_id]);
        if (empty($data)) return false;
        if (!is_array($data)) {
            $data = json_decode($data, true);
        }

        $batchInsert = [];
        $exhibitorService = new ExhibitorServices();
        foreach ($data as $val) {
            $model = new ExhibitionExhibitor();
            $model->exhibition_id = $exhibition_id;
            $model->exhibitor_id = $val;
            $exhibitor = $exhibitorService->getExhibitorsById($val);
            if (empty($exhibitor)) continue;
            $model->name = $exhibitor['name'];
            array_push($batchInsert, $model);
        }
        //批量添加数据
        if (!empty($batchInsert) ) {
            $errMsg = '';
            BaseActiveRecord::batchInsertSqlArray($batchInsert, $errMsg);
        }
        return true;
    }

    /**
     * 谁来参展
     * @param int $limit
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getExhibitionExhibitor($limit = 6) {
        $model = ExhibitionExhibitor::find(false);
        $table = ExhibitionExhibitor::tableName();
        $time = time();
        $res = $model->leftJoin(Exhibition::tableName().' as a', $table.".exhibition_id=a.id and a.status=".Exhibition::STATUS_FLAG_NORMAL)
            ->leftJoin(Exhibitor::tableName()." as b", $table.".exhibitor_id=b.id and b.status=".Exhibition::STATUS_FLAG_NORMAL)
            ->andFilterWhere(['=', $table.'.status', ExhibitionExhibitor::STATUS_FLAG_NORMAL])
            ->andFilterWhere(['>', "a.endtime", $time])
            ->groupBy($table.'.exhibition_id,'.$table.'.exhibitor_id')
            ->select($table.'.exhibition_id,'.$table.'.exhibitor_id,starttime,a.endtime,a.city,b.id,b.name,b.logo')
            ->asArray()
            ->limit($limit)
            ->all();

        //$sql = $model->createCommand()->getRawSql();

        return $res;
    }

    /**
     * 添加展会展商
     * @param $exhibition_id
     * @param $exhibitor
     * @return bool
     */
    public function addExhibitionExhibitor($exhibition_id, $exhibitor) {
        if (empty($exhibitor)) return false;
        $exhibitorList = explode(',', $exhibitor);
        $exhibitorList = array_filter($exhibitorList);
        $exhibitorList = array_unique($exhibitorList);

        if (!empty($exhibitorList)) {
            $exhibitorService = new ExhibitorServices();
            $batchInsert = [];
            foreach ($exhibitorList as $name) {
                $exhibitorInfo = $exhibitorService->getExhibitorsByName($name);
                if (empty($exhibitorInfo)) continue;
                $model = new ExhibitionExhibitor();
                $model->exhibition_id = $exhibition_id;
                $model->exhibitor_id = $exhibitorInfo['id'];
                $model->name = $exhibitorInfo['name'];
                array_push($batchInsert, $model);
            }
            //批量添加数据
            if (!empty($batchInsert) ) {
                BaseActiveRecord::batchInsertSqlArray($batchInsert);
            }
        }
    }
}
